package com.tes.jar;

import java.sql.*;

public class Db {

    // MySQL 8.0 以下版本 - JDBC 驱动名及数据库 URL
//    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
//    static final String DB_URL = "jdbc:mysql://localhost:3306/RUNOOB";

    // MySQL 8.0 以上版本 - JDBC 驱动名及数据库 URL
    static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
    protected String DB_URL = "jdbc:mysql://localhost:3306/RUNOOB?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";


    // 数据库的用户名与密码，需要根据自己的设置
    protected String USER = "root";
    protected String PASSWORD = "123456";
    protected String PORT = "3306";
    protected String Url = "127.0.0.1";
    protected String DBNAME = "db";

    /* 选填 */
    protected boolean useSSL = false;
    protected boolean allowPublicKeyRetrieval = true;
    protected String serverTimezone = "UTC";

    Connection conn = null;

    public static void main(String[] args) {

        String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
        String DB_URL = "jdbc:mysql://127.0.0.1:3306/db?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";

        Connection conn = null;
        Statement stmt = null;
        try{
            // 注册 JDBC 驱动
            Class.forName(JDBC_DRIVER);

            // 打开链接
            conn = DriverManager.getConnection(DB_URL,"root","root");

            // 执行查询
            System.out.println(" 实例化Statement对象...");
            stmt = conn.createStatement();
            String sql;
            sql = "SELECT `id`, `name`, `tel` FROM `biao`";
            ResultSet rs = stmt.executeQuery(sql);

            // 展开结果集数据库
            while(rs.next()){
                // 通过字段检索
                int id  = rs.getInt("id");
                String name = rs.getString("name");
                String tel = rs.getString("tel");

                // 输出数据
                System.out.print("ID: " + id);
                System.out.print(", 站点名称: " + name);
                System.out.print(", 站点 URL: " + tel);
                System.out.print("\n");
            }
            // 完成后关闭
            rs.close();
            stmt.close();
            conn.close();
        } catch(Exception se){
            // 处理 JDBC 错误
            se.printStackTrace();
        }// 处理 Class.forName 错误
        finally{
            // 关闭资源
            try{
                if(stmt!=null) stmt.close();
            }catch(SQLException ignored){
            }// 什么都不做
            try{
                if(conn!=null) conn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }
        }

    }

    public String test(){
        String USER = "xxx";
        USER+="ppppp";
        USER+="ppppp";
        USER+="ppppp";
        USER+="ppppp";
        return USER;
    }

    public void set() {

        Statement stmt = null;
        try{
            // 注册 JDBC 驱动
            Class.forName(JDBC_DRIVER);

            // 打开链接
            System.out.println("连接数据库...");
            this.conn = DriverManager.getConnection(DB_URL,USER,PASSWORD);

            // 执行查询
            System.out.println(" 实例化Statement对象...");
            stmt = conn.createStatement();
            String sql;
            sql = "SELECT id, name, url FROM websites";
            ResultSet rs = stmt.executeQuery(sql);

            // 展开结果集数据库
            while(rs.next()){
                // 通过字段检索
                int id  = rs.getInt("id");
                String name = rs.getString("name");
                String url = rs.getString("url");

                // 输出数据
                System.out.print("ID: " + id);
                System.out.print(", 站点名称: " + name);
                System.out.print(", 站点 URL: " + url);
                System.out.print("\n");
            }
            // 完成后关闭
            rs.close();
            stmt.close();
            conn.close();
        }catch(SQLException se){
            // 处理 JDBC 错误
            se.printStackTrace();
        }catch(Exception e){
            // 处理 Class.forName 错误
            e.printStackTrace();
        }finally{
            // 关闭资源
            try{
                if(stmt!=null) stmt.close();
            }catch(SQLException se2){
            }// 什么都不做
            try{
                if(conn!=null) conn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }
        }
        System.out.println("Goodbye!");

        Db mysql = new Db();
        String user = mysql.getUser();
        System.out.println(user);

    }




    //设置ip/mysql地址
    public Db setUrl(String url){
        this.Url = url;
        return this;
    }
    public String getUrl(){
        return this.Url;
    }
    public Boolean isSetUrl(){
        return this.isEmpty(this.Url);
    }

    //设置数据库名称
    public Db setDbName(String db_name){
        this.DBNAME = db_name;
        return this;
    }
    public String getDbName(){
        return this.DBNAME;
    }
    public Boolean isSetDbName(){
        return this.isEmpty(this.DBNAME);
    }


    //设置用户名
    public Db setUser(String user){
        this.USER = user;
        return this;
    }
    public String getUser(){
        return this.USER;
    }
    public Boolean isSetUser(){
        return this.isEmpty(this.USER);
    }

    //设置密码
    public Db setPassword(String password){
        this.PASSWORD = password;
        return this;
    }
    public String getPassword(){
        return this.PASSWORD;
    }
    public Boolean isSetPassword(){
        return this.isEmpty(this.PASSWORD);
    }

    //设置端口号
    public Db setPort(String port){
        this.PORT = port;
        return this;
    }
    public String getPort(){
        return this.PORT;
    }
    public Boolean isSetPort(){
        return this.isEmpty(this.PORT);
    }

    //设置ssl开关
    public Db setUseSSL(boolean useSSL){
        this.useSSL = useSSL;
        return this;
    }
    public boolean getUseSSL(){
        return this.useSSL;
    }

    //允许公共检索
    public Db setAllowPublicKeyRetrieval(boolean allowPublicKeyRetrieval){
        this.allowPublicKeyRetrieval = allowPublicKeyRetrieval;
        return this;
    }
    public boolean getAllowPublicKeyRetrieval(){
        return this.allowPublicKeyRetrieval;
    }

    //设置时区
    public Db setServerTimezone(String serverTimezone){
        this.serverTimezone = serverTimezone;
        return this;
    }
    public String getServerTimezone(){
        return this.serverTimezone;
    }
    public Boolean isSetServerTimezone(){
        return this.isEmpty(this.serverTimezone);
    }


    /**
     * 生成链接
     * @return
     */
    protected  Boolean creamDbUrl(){

        //校验传入参数
        boolean check =  this.isNotEmptyBatch(this.Url,this.PORT,this.DBNAME);
        if (!check) return false;

        this.DB_URL = "jdbc:mysql://"+ this.Url+":"+ this.PORT +"/"
                + this.DBNAME
                +"?useSSL="+ this.BoolToString(this.useSSL)
                +"&allowPublicKeyRetrieval="+ this.BoolToString(this.allowPublicKeyRetrieval)
                +"&serverTimezone="+ this.serverTimezone;
        return true;
    }

    //(可变参数)批量判断
    protected  boolean isNotEmptyBatch(String... strs){
        for (String str : strs) {
            if(str==null || "".equals(str))
                return false;
        }
        return true;
    }

    //验证字符串
    protected  Boolean isEmpty(String str) {
        return (str == null || str.trim().length() == 0);
    }

    //判断字符串是不是数字
    protected  Boolean checkNumber(String str) {
        return !isEmpty(str) && str.matches("^\\d+$");
    }

    //逻辑型转字符串
    protected String BoolToString(Boolean bool){
        return bool ? "true" : "false";
    }
}
